import Axios from 'axios';

export class APIRequester {
  private jsonParser: Parse;

  constructor(jsonParser: Parse) {
    this.jsonParser = jsonParser;
  }

  public setParser(jsonParser: Parse): void {
    this.jsonParser = jsonParser;
  }

  public async fetchData(url: string, postObject: object): Promise<string[] | null> {
    const response = await Axios.post(url, postObject, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    });
    if (response.data['error_code'] && response.data['error_code'] !== '52000') {
      return null;
    }
    return this.jsonParser.parse(response.data);
  }
}
